﻿using System;
using System.Windows;
using System.Reflection;
using System.Reflection.Emit;

namespace MapSurface.AssemblyReflexion
{
    public class MethodBuilder
    {
        private AppDomain myDomain;
        AssemblyBuilder myAsmBuilder;
        ModuleBuilder myModule;
        TypeBuilder myTypeBld;
        public MethodBuilder(string assName)
        {
            myDomain = AppDomain.CurrentDomain;
            AssemblyName asmName = new AssemblyName();
            asmName.Name = assName;
            myAsmBuilder = myDomain.DefineDynamicAssembly(asmName, AssemblyBuilderAccess.RunAndSave);
            myModule = myAsmBuilder.DefineDynamicModule("MyDynamicAsm", "MyDynamicAsm.dll");
            myTypeBld = myModule.DefineType("MyDynamicType", TypeAttributes.Public);
        }

        private void AddMethodDynamically(string mthdName,
                                                 Type[] mthdParams,
                                                 Type returnType)
        {

            System.Reflection.Emit.MethodBuilder myMthdBld = myTypeBld.DefineMethod(
                                                 mthdName,
                                                 MethodAttributes.Public,
                                                 returnType,
                                                 mthdParams);

            ILGenerator ILout = myMthdBld.GetILGenerator();
        }


        public void Addhandler(string name)
        {
            Type[] para = { typeof(object), typeof(RoutedEventArgs) };
            AddMethodDynamically(name, para, typeof(void));
        }
    }
    
}